﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Admin.Models;
using MvcSiteMapProvider;
using System.Text.RegularExpressions;
using PagedList;
using Admin.Common;
namespace Admin.Controllers
{
    public class HotelsController : Admin.Common.BaseController
    {
        private BookingDBEntities db = new BookingDBEntities();
        //
        // GET: /Hotels/
        public ActionResult Index()
        {
            return RedirectToAction("Index","Home");
        }

        [HttpPost]
        public ActionResult Index(int? PageSize,string destination, DateTime? checkInDate, FormCollection frmCol)
        {
            char[] spt = { ',',' ' };

            if (Session["destination"]==null)
            {
                Session["destination"] = "";
            }

            string[] _destination = Session["destination"].ToString().Split(spt); 

            if (!string.IsNullOrEmpty(destination))
            {
                Session["destination"] = destination;
                _destination = Session["destination"].ToString().Split(spt); 
            }

            var tblhotels = db.TblHotels.Where(t => t.IsActive == true).Include(t => t.TblCity).
                Include(t => t.TblCountry).
                Include(t => t.TblUserProfile);

            var tblHotelFilter = tblhotels;

            foreach (var item in _destination) // voi moi tu khoa duoc tach ra tu session tren, ta tien hanh loc tiep khach san theo logic ||
            {
                var temp = tblHotelFilter.Where(x => x.HotelName.Contains(item) || x.TblCity.CityName.Contains(item)||x.StreetAddress.Contains(item)) ;
                if (temp.ToList().Count > 0)
                {
                    tblHotelFilter = temp;
                }
            }


            if (frmCol["lvl1"] != null && frmCol["lvl1"].ToString().Contains("true"))
            {
                tblHotelFilter = tblHotelFilter.Where(x => x.StarRating == 1);
            }
            if (frmCol["lvl2"]!=null&&frmCol["lvl2"].ToString()=="true")
            {
                tblHotelFilter = tblHotelFilter.Where(x => x.StarRating == 2);
            }
            if (frmCol["lvl3"]!=null&&frmCol["lvl3"].ToString() == "true")
            {
                tblHotelFilter = tblHotelFilter.Where(x => x.StarRating == 3);
            }
            if (frmCol["lvl4"]!=null&&frmCol["lvl4"].ToString() == "true")
            {
                tblHotelFilter = tblHotelFilter.Where(x => x.StarRating == 4);
            }
            if (frmCol["lvl5"]!=null&&frmCol["lvl5"].ToString() == "true")
            {
                tblHotelFilter = tblHotelFilter.Where(x => x.StarRating == 5);
            }

            ViewBag.TotalHotel = string.Format("Có {0} khách sạn trên tổng số {1}", tblHotelFilter.Count().ToString(), tblhotels.Count().ToString());

            if (Request.IsAjaxRequest())
            {
                return PartialView(tblHotelFilter.OrderBy(z => z.HotelName).ToList().ToPagedList(PageSize ?? 1, 10));
            }

            return PartialView(tblHotelFilter.OrderBy(z => z.HotelName).ToList().ToPagedList(PageSize ?? 1, 10));
        }

        //
        // GET: /Hotels/Details/5
        [MvcSiteMapNode(Title = "Details", ParentKey = "Hotels")]
        public ActionResult Details(int? id)
        {
            TblHotel tblhotel = db.TblHotels.Find(id);
            if (tblhotel == null)
            {
                return HttpNotFound();
            }
            return View(tblhotel);
        }

        //
        // GET: /Hotels/Create
         [MvcSiteMapNode(Title = "Create", ParentKey = "Hotels")]
        public ActionResult Create()
        {
            ViewBag.CityID = new SelectList(db.TblCities, "CityID", "CityName");
            ViewBag.CountryID = new SelectList(db.TblCountries, "CountryID", "CountryName");
            ViewBag.ContactNameID = new SelectList(db.TblUserProfiles, "UserID", "UserName");
            return View();
        }

        //
        // POST: /Hotels/Create

        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Create(TblHotel tblhotel)
        {
            if (ModelState.IsValid)
            {
                db.TblHotels.Add(tblhotel);
                db.SaveChanges();
                return RedirectToAction("Index");
            }

            ViewBag.CityID = new SelectList(db.TblCities, "CityID", "CityName", tblhotel.CityID);
            ViewBag.CountryID = new SelectList(db.TblCountries, "CountryID", "CountryName", tblhotel.CountryID);
            ViewBag.ContactNameID = new SelectList(db.TblUserProfiles, "UserID", "UserName", tblhotel.ContactNameID);
            return View(tblhotel);
        }

        //
        // GET: /Hotels/Edit/5

        public ActionResult Edit(int id = 0)
        {
            TblHotel tblhotel = db.TblHotels.Find(id);
            if (tblhotel == null)
            {
                return HttpNotFound();
            }
            ViewBag.CityID = new SelectList(db.TblCities, "CityID", "CityName", tblhotel.CityID);
            ViewBag.CountryID = new SelectList(db.TblCountries, "CountryID", "CountryName", tblhotel.CountryID);
            ViewBag.ContactNameID = new SelectList(db.TblUserProfiles, "UserID", "UserName", tblhotel.ContactNameID);
            return View(tblhotel);
        }

        //
        // POST: /Hotels/Edit/5

        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Edit(TblHotel tblhotel)
        {
            if (ModelState.IsValid)
            {
                db.Entry(tblhotel).State = EntityState.Modified;
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            ViewBag.CityID = new SelectList(db.TblCities, "CityID", "CityName", tblhotel.CityID);
            ViewBag.CountryID = new SelectList(db.TblCountries, "CountryID", "CountryName", tblhotel.CountryID);
            ViewBag.ContactNameID = new SelectList(db.TblUserProfiles, "UserID", "UserName", tblhotel.ContactNameID);
            return View(tblhotel);
        }

        //
        // GET: /Hotels/Delete/5

        public ActionResult Delete(int id = 0)
        {
            TblHotel tblhotel = db.TblHotels.Find(id);
            if (tblhotel == null)
            {
                return HttpNotFound();
            }
            return View(tblhotel);
        }

        //
        // POST: /Hotels/Delete/5

        [HttpPost, ActionName("Delete")]
        [ValidateAntiForgeryToken]
        public ActionResult DeleteConfirmed(int id)
        {
            TblHotel tblhotel = db.TblHotels.Find(id);
            db.TblHotels.Remove(tblhotel);
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }
    }
}